#pragma once
#include "JStage.h"
#include <map>
#include <vector>
class JGraphOrigin;
class JPiece;
class JPoint;
using namespace std;

struct JPaneStagePos
{
	CPoint			ptCenterPos;	
	float			fRotate;			//rad
	vector<CPoint>	ptPosShown;			//
	vector<CPoint>	vCtrlPoints;
	CRect			rcBoundaryBoxShown;
};

class JPaneStage :
	public JStage
{
public:
	JPaneStage();
	~JPaneStage();

private:
	friend class JPaneStageDCRender;

	JGraphOrigin*					m_pCurGraphOrigin;
	map<JPiece*, JPaneStagePos*>	m_mapPiecePoses;
	vector<JPiece*>					m_vPieces;

	JPiece*							m_pCurPiece;
	JPiece*							m_pHotPiece;

	CRect	m_destRc;
	float	m_fScaleX, m_fScaleY;

	int		m_nStageWidth, m_nStageHeight;

	void	Start(JGraphOrigin* _pGraphOrigin,
		CRect& _rcStage,
		float _imageScaleX,
		float _imageScaleY);

	void	Start(JGraphOrigin* _pGraphOrigin,
		CRect& _rcStage, 
		CImage* _pImage);

	void			GetBoundaryBoxShown(JPiece* _pPiece,
		CRect& _rc);

	CPoint			GetPiecePointPos(JPiece* _pPiece,
		JPoint* _pPoint);
public:
	virtual bool Start(JGame* _pGame, CRect& _rc);
	virtual void Stop();
	virtual bool OnImageLoaded(JGame* _pGame,
		CRect& _rc);
	void	Cleanup();

	JPaneStagePos*	GetPiecePos(JPiece* _pPiece);

	virtual int		GetPieceCountInStage();
	virtual JPiece*	GetPieceInStage(int _idx);

	JPiece*			GetHotPiece();
	JPiece*			GetCurPiece();
	void			SetHotPiece(JPiece* _pPiece);
	void			SetCurPiece(JPiece* _pPiece);
	bool			IsCurPiece(JPiece* _pPiece);
	bool			IsHotPiece(JPiece* _pPiece);

	JPiece*			GetPieceByPos(CPoint _pos);
	bool			IsInPiece(JPiece* _pPiece, CPoint _pos);

	void			UpdatePosShown(JPiece* _pPiece);

	//return game finished
	bool			OnPieceMoveFinish(JPiece* _pPiece);

	bool			IsStarted();
	
	int				GetStageWidth() { return m_nStageWidth; }
	int				GetStageHeight() { return m_nStageHeight; }

	void			GetBoundaryBoxShown(JPiece* _pPiece,
		CRect& _rc, int _nInflate);

	bool			CheckForFinish();
};

